﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Xml;

namespace Tools.Framework
{
    public static class RequestBaseHelper
    {
        /// <summary>
        /// 获取上下文所有参数
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static Dictionary<string, object> ParametersDictionary(this HttpRequestBase request)
        {
            var parametersKeys = request.Params.AllKeys.ToList().Where(a => !string.IsNullOrEmpty(a) && !string.IsNullOrWhiteSpace(a));
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            foreach (string namekey in parametersKeys)
            {
                string value = request[namekey];
                if (XmlHelper.IsXmlDoc(value) && !string.IsNullOrEmpty(value))
                {
                    var dictory = XmlHelper.XmlToDictionary(value);
                    dictory.ToList().ForEach(a =>
                    {
                        parameters.Add(a.Key, a.Value);
                    });
                }
                else
                {
                    parameters.Add(namekey, request[namekey]);
                }
            }
            return parameters;
        }


        /// <summary>
        /// 获取上下文所有参数
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static Dictionary<string, object> ParametersDictionary(this HttpRequest request)
        {
            var parametersKeys = request.Params.AllKeys.ToList().Where(a => !string.IsNullOrEmpty(a) && !string.IsNullOrWhiteSpace(a));
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            foreach (string namekey in parametersKeys)
            {
                string value = request[namekey];
                if (XmlHelper.IsXmlDoc(value) && !string.IsNullOrEmpty(value))
                {
                    var dictory = XmlHelper.XmlToDictionary(value);
                    dictory.ToList().ForEach(a =>
                    {
                        if (!parameters.ContainsKey(a.Key))
                        {
                            parameters.Add(a.Key, a.Value);
                        }
                    });
                }

                if (!parameters.ContainsKey(namekey))
                {
                    parameters.Add(namekey, request[namekey]);
                }
            }
            return parameters;
        }

    }
}
